<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :before-close="handleClose"
    :title="title"
    width="40%">
    <dynForm
      ref="dataForm"
      :info-form="searchForm"
      :rules="rules"
      :search-tags="searchTags"
      :is-inline="false"
      :select-list="selectList"/>
    <div class="trend-body">
      <span>连续</span>
      <el-input
        :style="{width: '100px'}"
        v-model="searchForm.input"
        clearable/>
      <span>点,</span>
      <span>呈</span>
      <el-select v-model="searchForm.value" placeholder="请选择"/>
      <span>趋势</span>
    </div>
    <span slot="footer">
      <el-button icon="el-icon-circle-close" @click="handleClose">{{
        $t("cancel")
      }}</el-button>
      <el-button
        v-btnThrottle="doSave"
        type="primary"
        icon="el-icon-receiving"
      >{{
        $t("save")
      }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { doAdd, doUpdate, getInfos } from '@/api/alarm/level'
import { getAllActions } from '@/api/alarm/action'

export default {
  name: 'RulesSettingModel',
  data() {
    return {
      title: '',
      dialogVisible: false,
      searchForm: {},
      searchTags: [
        { label: this.$t('rulesName'), prop: 'rulesName', tag: 'DynInput' },
        { label: this.$t('rulesType'), prop: 'rulesType', tag: 'DynSelect' }
      ],
      selectList: {},
      rules: {
        actions: [
          { required: true, message: `${this.$t('pleaseSelect')}${this.$t('alarmAction')}`, trigger: 'change' }
        ],
        alarm_Level: [{ required: true, message: `${this.$t('pleaseInput')}${this.$t('AlarmLevel')}`, trigger: 'blur' }]
      }
    }
  },
  methods: {
    doSave() {
      this.$refs['dataForm'].$refs['dynForm'].validate((valid) => {
        if (valid) {
          if (this.searchForm.id) {
            doUpdate(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleUpdate, this.handleClose)
            })
          } else {
            doAdd(this.$clearNull(this.searchForm)).then(res => {
              this.$betterTips(res, this.handleUpdate, this.handleClose)
            })
          }
        }
      })
    },
    async handleOpen(row) {
      if (row && row.id) {
        const res = await getInfos({ id: row.id })
        this.searchForm = res.data
        this.title = this.$t('modify')
      } else {
        this.title = this.$t('add')
      }
      this.getLevelList()
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].$refs['dynForm'].clearValidate()
      })
    },
    handleClose() {
      Object.assign(this.$data, this.$options.data.call(this))
    },
    // 触发父组件更新
    handleUpdate() {
      this.$emit('initList')
    },
    async getLevelList() {
      const res = await getAllActions()
      this.$set(this.selectList, 'alarmAction', res.data)
    }
  }
}
</script>

<style scoped lang="scss">
.trend-body{
    display: flex;
    justify-content: center;
    align-items: center;
    span{
        padding: 0 10px;
    }
}
</style>
